package com.dy.lock.screen.component.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dy.lock.screen.component.AddAttentionComponent;
import com.dy.lock.screen.constants.TransmissionCode;
import com.dy.lock.screen.exception.ServerDisponseException;
import com.dy.lock.screen.pojo.CustomerAttentionInfo;
import com.dy.lock.screen.scoket.Head;
import com.dy.lock.screen.service.CustomerAttentionService;
import com.dy.lock.screen.service.UserService;
import com.dy.lock.screen.utils.CommonUtil;
import com.google.gson.Gson;

@Component("addAttentionComponent")
public class AddAttentionComponentImpl implements AddAttentionComponent {

	private static final Logger log = Logger
			.getLogger(AddAttentionComponentImpl.class);
	private Gson gson;

	@Resource(name = "userService")
	public UserService userService;
	@Resource(name = "customerAttentionService")
	private CustomerAttentionService customerAttentionService;

	public AddAttentionComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public boolean addAttention(String body, Head head) {
		String vo = gson.fromJson(body, String.class);
		if (StringUtils.isNotBlank(vo)) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					Map<String, Object> map = new HashMap<String, Object>();
					Integer totalCount = userService.queryCount(map);
					Integer currentPage = 1;
					Integer pageSize = 200;
					// 计算总页数
					Integer pageCount = (int) Math.ceil(totalCount
							/ (double) pageSize);
					for (int i = 0; i < pageCount; i++) {
						currentPage = i + 1;
						Integer pageStart = 1;
						Integer pageEnd = 50;
						if (currentPage != null && pageSize != null) {
							pageStart = (currentPage - 1) * pageSize + 1;
							pageEnd = currentPage * pageSize;
						}
						Map<String, Object> para = new HashMap<String, Object>();
						para.put("pageStart", pageStart);
						para.put("pageEnd", pageEnd);
						List<String> userIds = userService.getUserIdList(para);
						if (userIds != null && !userIds.isEmpty()) {
							for (String userId : userIds) {
								CustomerAttentionInfo attInfo = new CustomerAttentionInfo();
								attInfo.setAttentionId(userId);
								Integer pageStartRandom = new Random()
										.nextInt(totalCount);
								Integer pageEndRandom = new Random()
										.nextInt(totalCount);
								while (true) {
									pageStartRandom = new Random()
											.nextInt(totalCount);
									pageEndRandom = new Random()
											.nextInt(totalCount);
									if (pageStartRandom != null
											&& pageEndRandom != null) {
										if ((pageEndRandom > pageStartRandom)
												&& (pageEndRandom - pageStartRandom) >= 100
												&& (pageEndRandom - pageStartRandom) <= 500) {
											log.info("pageStartRandom"
													+ pageStartRandom
													+ ",  pageEndRandom"
													+ pageEndRandom);
											break;
										}
									} else {
										continue;
									}
								}

								Map<String, Object> para2 = new HashMap<String, Object>();
								para2.put("pageStart", 1);
								para2.put("pageEnd", Integer.MAX_VALUE);
								List<String> beConcernedIds = userService
										.getUserIdList(para2);
								beConcernedIds = beConcernedIds.subList(
										pageStartRandom, pageEndRandom);
								if (beConcernedIds != null
										&& !beConcernedIds.isEmpty()) {
									for (String beConceredId : beConcernedIds) {
										if (!beConceredId.equals(userId)) {
											attInfo
													.setBeConcernedId(beConceredId);
											CustomerAttentionInfo att = customerAttentionService
													.queryByAidAndBeid(attInfo);
											if (att == null) {
												customerAttentionService
														.save(attInfo);
											} else {
												continue;
											}
										} else {
											continue;
										}
									}
								}
							}
						}

					}
				}

			}).start();

		} else {
			log.info("requestId:" + head.getRequestId() + ",测试人工加关注时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
		return true;
	}

	public static void main(String[] args) {
		while (true) {
			Integer pageStartRandom = new Random().nextInt(122);
			Integer pageEndRandom = new Random().nextInt(122);
			if (pageStartRandom != null && pageEndRandom != null) {
				if ((pageEndRandom - pageStartRandom) >= 10
						&& (pageEndRandom - pageStartRandom) <= 50) {
					System.out.println("pageStartRandom" + pageStartRandom
							+ ",  pageEndRandom" + pageEndRandom);
					break;
				}
			} else {
				continue;
			}
		}
	}
}
